package com.airspyar;

import com.airspyar.R;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SlidingDrawer;
import android.widget.TextView;

public class Display {
	private final Activity parent;
	private final TextView callsignView;
	private final TextView aircraftView;
	private final TextView regView;
	private final TextView hexView;
	private final TextView airlineView;
	private final TextView fromView;
	private final TextView toView;
	private final TextView posView;
	private final TextView altitudeView;
	private final TextView speedView;
	private final TextView trackView;
	private final TextView distanceView;
	private final TextView authorView;
	private final SlidingDrawer slidingDrawer;
	private final RadarView radar;
	private final CameraView camera;
	private final ImageView imageView;
	private final LinearLayout dataView;
	private View loadingDialog;
	
	private final Animation fadeIn, fadeOut, leftIn, rightOut;
	
	public Display(Activity parent){
		this.parent=parent;
		callsignView=(TextView)parent.findViewById(R.id.textView_Callsign);
		aircraftView=(TextView)parent.findViewById(R.id.textView_Aircraft);
		regView=(TextView)parent.findViewById(R.id.textView_Reg);
		hexView=(TextView)parent.findViewById(R.id.textView_Hex);
		airlineView=(TextView)parent.findViewById(R.id.textView_Airline);
		fromView=(TextView)parent.findViewById(R.id.textView_From);
		toView=(TextView)parent.findViewById(R.id.textView_To);
		posView=(TextView)parent.findViewById(R.id.textView_Pos);
		altitudeView=(TextView)parent.findViewById(R.id.textView_Altitude);
		speedView=(TextView)parent.findViewById(R.id.textView_Speed);
		trackView=(TextView)parent.findViewById(R.id.textView_Track);
		distanceView=(TextView)parent.findViewById(R.id.textView_Distance);
		authorView=(TextView)parent.findViewById(R.id.textView_Author);
		dataView=(LinearLayout)parent.findViewById(R.id.dataView);
		
		camera=(CameraView)parent.findViewById(R.id.cameraView);
		
		slidingDrawer=(SlidingDrawer)parent.findViewById(R.id.drawer);
		slidingDrawer.lock();

		radar=(RadarView)parent.findViewById(R.id.radar);

		imageView=(ImageView)parent.findViewById(R.id.imageView);
		
		fadeIn=AnimationUtils.loadAnimation(parent, android.R.anim.fade_in);
		fadeOut=AnimationUtils.loadAnimation(parent, android.R.anim.fade_out);
		leftIn=AnimationUtils.loadAnimation(parent, android.R.anim.slide_in_left);
		rightOut=AnimationUtils.loadAnimation(parent, android.R.anim.slide_out_right);
		

        LayoutInflater inflater=parent.getLayoutInflater();
        loadingDialog=inflater.inflate(R.layout.loading, null);
        parent.addContentView(loadingDialog, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        loadingDialog.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {}
        });
	}
	
	public CameraView getCamera(){
		return camera;
	}
	
	public RadarView getRadar(){
		return radar;
	}
	
	private void showView(View v){
		showView(v, fadeIn);
	}
	
	private void showView(View v, Animation animation){
		if(v.getVisibility()==View.VISIBLE)
			return;

		v.startAnimation(animation);

		v.setVisibility(View.VISIBLE);
	}
	
	private void hideView(View v){
		hideView(v, fadeOut);
	}
	
	private void hideView(View v, Animation animation){
		if(v.getVisibility()==View.GONE)
			return;
		
		v.startAnimation(animation);
		v.setVisibility(View.GONE);
	}
	
	public void hideAll(){
		hideView(dataView, rightOut);
		
		hideImage();
		
		showView(radar, leftIn);
	}
	
	public void showState(DetailedFlightState state){
		hideView(radar, rightOut);

		//Callsign
		if(state.getCallsign().length()==0){
			hideView(callsignView);
		}
		else{
			callsignView.setText(Html.fromHtml("<b>Callsign:</b> "+state.getCallsign()));
			showView(callsignView);
		}
		
		//Aircraft
		if(state.getAircraft().length()==0){
			hideView(aircraftView);
		}
		else{
			aircraftView.setText(Html.fromHtml("<b>Aircraft:</b> "+state.getAircraft()));
			showView(aircraftView);
		}
		
		if(state.getReg().length()==0){
			hideView(regView);
		}
		else{
			regView.setText(Html.fromHtml("<b>Reg:</b> "+state.getReg()));
			showView(regView);
		}	
		
		if(state.getHex().length()==0){
			hideView(hexView);
		}
		else{
			hexView.setText(Html.fromHtml("<b>Hex code:</b> "+state.getHex()));
			showView(hexView);
		}
		
		//Airlines
		if(state.getAirline().length()==0){
			hideView(airlineView);
		}
		else{
			airlineView.setText(Html.fromHtml("<b>Airline:</b> "+state.getAirline()));
			showView(airlineView);
		}
		
		//From
		if(state.getFrom().length()==0){
			hideView(fromView);
		}
		else{
			fromView.setText(Html.fromHtml("<b>From:</b> "+state.getFrom()));
			showView(fromView);
		}
		
		//To
		if(state.getTo().length()==0){
			hideView(toView);
		}
		else{
			toView.setText(Html.fromHtml("<b>To:</b> "+state.getTo()));
			showView(toView);
		}		
		
		//Position
		posView.setText(Html.fromHtml("<b>Position:</b> "+state.getLocation().getLat()+" / "+state.getLocation().getLong()));
		showView(posView);
		
		//Altitude
		altitudeView.setText(Html.fromHtml("<b>Altitude:</b> "+Math.round(state.getLocation().getAltitude()*1000)/1000f+"km"));
		showView(altitudeView);
		
		//Speed
		speedView.setText(Html.fromHtml("<b>Speed:</b> "+state.getSpeed()+"km/h"));
		showView(speedView);
		
		//Track
		trackView.setText(Html.fromHtml("<b>Track:</b> "+state.getTrack()+"\u00b0"));
		showView(trackView);
		
		//Distance
		distanceView.setText(Html.fromHtml("<b>Distance from you:</b> "+Math.round(state.getDistance()*1000)/1000f+"km"));
		showView(distanceView);

		
		showImage(state.getBitmap(), state.getBigImageUrl(), state.getAuthor());
		
		showView(dataView, leftIn);
	}
	
	private void showImage(Bitmap bitmap, final String bigImgUrl, String author){
		if(bitmap==null){
			hideImage();
		}
		else{
			BitmapDrawable drawable=new BitmapDrawable(bitmap);
			drawable.setDither(true);
			drawable.setFilterBitmap(true);
			imageView.setImageDrawable(drawable);
			imageView.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View v) {
					Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(bigImgUrl));
					parent.startActivity(browserIntent);	
				}});
			if(author!=null)
				authorView.setText("@"+author.toUpperCase());
			slidingDrawer.unlock();
		}
	}
	
	private void hideImage(){
		if(slidingDrawer.isOpened())
			slidingDrawer.animateClose();
		
		slidingDrawer.lock();
	}
	
	public void showLoadingDialog(){
		showView(loadingDialog);
	}
	
	public void hideLoadingDialog(){
		hideView(loadingDialog);
	}
}
